//
//  YXRegistViewController.m
//  跑跑快销
//
//  Created by 杨潇 on 14-4-28.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "YXRegistViewController.h"

@interface YXRegistViewController ()

@end

@implementation YXRegistViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _tradeArr = [[NSArray alloc] initWithObjects:@"烟酒类",@"饮品",@"乳制品",@"调味品",@"粮油类",@"冷冻食品类",@"方便食品类",@"休闲食品类",@"保健品类",@"家居日用品类",@"化妆品类",@"服帽类",@"玩具类",@"办公用品类",@"其他(其他行业)", nil];
    YXRegistView * registView = [[YXRegistView alloc] init];
    self.view = registView;
    _registView = registView;
    [_registView.getAuthCodeBtn addTarget:self action:@selector(touchGetAuthCodeBtn) forControlEvents:UIControlEventTouchUpInside];
    [_registView.backButton addTarget:self action:@selector(touchBackButtn) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tradeArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * identifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [_tradeArr objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _tradeStr = [[NSString stringWithFormat:@"%d",indexPath.row] copy];
    _tabBGBtn.hidden = YES;
    [_registView.selectBtn setTitle:[_tradeArr objectAtIndex:indexPath.row] forState:UIControlStateNormal];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView animateWithDuration:.25 animations:^{
        _registView.inputAuthCodeScroll.frame = CGRectMake(0, 0, 300, 144);
    }];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    _registView.inputAuthCodeScroll.frame = CGRectMake(0, 0, 300, 360);
}

#pragma mark - ButtonsClick
- (void)touchGetAuthCodeBtn
{
    if (_registView.inputNumberText.text.length != 11) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入正确的手机号码。"];
        return;
    }
    
    [NSURLConnection sendRequestWithHeadStr:[[YXWindowInfo sharedWindowInfo] requestUrlStr] headValue:nil serviceToken:nil text:[NSDictionary dictionaryWithObjectsAndKeys:@"sendCode",@"type",_registView.inputNumberText.text,@"phone", nil] dataBlock:^(NSDictionary *dic) {
        NSString * resultCode = [dic objectForKey:@"resultCode"];
        if ([resultCode isEqualToString:@"0"]) {
            [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"发送验证码成功，请查看你的手机。"];
            _phoneNumberStr = [_registView.inputNumberText.text copy];
            [_registView createUpView2];
            [_registView.nextBtn addTarget:self action:@selector(touchNextBtn) forControlEvents:UIControlEventTouchUpInside];
            _registView.yLifeText.delegate = self;
            [_registView.yLifeText addTarget:self action:@selector(down) forControlEvents:UIControlEventEditingDidEndOnExit];
            _registView.inputAuthCodeText.delegate = self;
            [_registView.inputAuthCodeText addTarget:self action:@selector(down) forControlEvents:UIControlEventEditingDidEndOnExit];
            _registView.inputPasswordText.delegate = self;
            [_registView.inputPasswordText addTarget:self action:@selector(down) forControlEvents:UIControlEventEditingDidEndOnExit];
            _registView.affirmPasswordText.delegate = self;
            [_registView.affirmPasswordText addTarget:self action:@selector(down) forControlEvents:UIControlEventEditingDidEndOnExit];
            _registView.inputPasswordText.delegate = self;
            [_registView.inputPasswordText addTarget:self action:@selector(down) forControlEvents:UIControlEventEditingDidEndOnExit];
        }else{
            [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:[dic objectForKey:@"errorDesc"]];
        }
    }];
}

- (void)down
{
    
}

- (void)touchBackButtn
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)touchNextBtn
{
    if ([_registView.yLifeText.text isEqualToString:@""]) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入激活码。"];
        return;
    }
    if ([_registView.inputAuthCodeText.text isEqualToString:@""]) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入验证码。"];
        return;
    }
    if ([_registView.inputPasswordText.text isEqualToString:@""]) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入密码。"];
        return;
    }
    if (![_registView.affirmPasswordText.text isEqualToString:_registView.inputPasswordText.text]) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"两次输入密码不一致。。"];
        return;
    }
    
    [NSURLConnection sendRequestWithHeadStr:[[YXWindowInfo sharedWindowInfo] requestUrlStr] headValue:nil serviceToken:nil text:[NSDictionary dictionaryWithObjectsAndKeys:@"setAccount",@"type",_phoneNumberStr,@"phone",_registView.yLifeText.text,@"ActivationNo",_registView.inputAuthCodeText.text,@"code",_registView.inputPasswordText.text,@"passWord", nil] dataBlock:^(NSDictionary *dic) {
        NSString * resultCode = [dic objectForKey:@"resultCode"];
        if ([resultCode isEqualToString:@"0"]) {
            _userID = [[dic objectForKey:@"userId"] copy];
            [_registView createUpView3];
            [_registView.selectBtn addTarget:self action:@selector(touchSelectBtn) forControlEvents:UIControlEventTouchUpInside];
            [_registView.affirmRegisterBtn addTarget:self action:@selector(touchAffirmRegisterBtn) forControlEvents:UIControlEventTouchUpInside];
            [_registView.nameTf addTarget:self action:@selector(down) forControlEvents:UIControlEventEditingDidEndOnExit];
        }else{
            [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:[dic objectForKey:@"errorDesc"]];
        }

    }];
}

- (void)touchSelectBtn
{
    if (!_tabBGBtn) {
        _tabBGBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _tabBGBtn.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.4];
        _tabBGBtn.tag = 6;
        [_tabBGBtn addTarget:self action:@selector(touchTabBGBtn) forControlEvents:UIControlEventTouchUpInside];
        _tabBGBtn.frame = self.view.frame;
        [self.view addSubview:_tabBGBtn];
        
        UITableView * tradeTab = [[UITableView alloc] initWithFrame:CGRectMake(50, 50, 220, ([UIScreen mainScreen].bounds.size.height - 100)) style:UITableViewStylePlain];
        tradeTab.delegate = self;
        tradeTab.dataSource = self;
        [_tabBGBtn addSubview:tradeTab];
    }
    _tabBGBtn.hidden = NO;
}

- (void)touchTabBGBtn
{
    _tabBGBtn.hidden = YES;
}

- (void)touchAffirmRegisterBtn
{
    if (_registView.nameTf.text.length == 0) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入用户名称。"];
        return;
    }
    if ([_registView.selectBtn.titleLabel.text isEqualToString:@"选择行业"]) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请选择行业"];
        return;
    }
    [NSURLConnection sendRequestWithHeadStr:[[YXWindowInfo sharedWindowInfo] requestUrlStr] headValue:nil serviceToken:nil text:[NSDictionary dictionaryWithObjectsAndKeys:@"setUserInfo",@"type",_userID,@"userId",_tradeStr,@"pId",_registView.nameTf.text,@"name", nil] dataBlock:^(NSDictionary *dic) {
        NSLog(@"%@",dic);
        NSString * resultCode = [dic objectForKey:@"resultCode"];
        if ([resultCode isEqualToString:@"0"]) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:[dic objectForKey:@"errorDesc"]];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
